package com.mtw.bbs.auth.provider;

import com.mtw.bbs.common.core.exception.BaseException;
import com.mtw.bbs.common.core.util.SpringUtil;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@AllArgsConstructor
public class TokenProviderBuilder {

	/**
	 * TokenGranter缓存池
	 */
	private static final Map<String, TokenProvider> GRANTER_POOL = new ConcurrentHashMap<>();

	static {
		GRANTER_POOL.put(PasswordTokenProvider.GRANT_TYPE, SpringUtil.getBean(PasswordTokenProvider.class));
	}

	/**
	 * 获取TokenGranter
	 *
	 * @param grantType 授权类型
	 * @return ITokenGranter
	 */
	public static TokenProvider getTokenProvider(String grantType) {
		TokenProvider tokenProvider = GRANTER_POOL.get(grantType);
		if (tokenProvider == null) {
			throw new BaseException("no grantType was found");
		} else {
			return tokenProvider;
		}
	}

}
